﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using WebInstagram.DAL;
using WebInstagram.Models;
using AutoMapper;
using WebInstagram.ViewModels;
namespace WebInstagram.API
{
    public class PhotosController : ApiController
    {
        private Context db = new Context();

        public IEnumerable<PhotoLessView> GetRecentPhotos()
        {
            PhotosRep rep = new PhotosRep();
            var photos = rep.Table.OrderByDescending(p => p.Created).AsEnumerable();

            var viewPhotos = new List<PhotoLessView>();
            Mapper.CreateMap<Photo, PhotoLessView>();
            foreach (var photo in photos)
            {
                var view = new PhotoLessView();
                view = Mapper.Map<Photo, PhotoLessView>(photo, view);
                viewPhotos.Add(view);
            }

            return viewPhotos.AsEnumerable();

        }

        // GET: api/Photos
        public IEnumerable<PhotoLessView> GetUserPhotos(string User)
        {
            PhotosRep rep = new PhotosRep();
            var photos = rep.GetUserPhotos(User);

            var viewPhotos = new List<PhotoLessView>();
            Mapper.CreateMap<Photo, PhotoLessView>();
            foreach (var photo in photos)
            {
                var view = new PhotoLessView();
                view = Mapper.Map<Photo, PhotoLessView>(photo, view);
                viewPhotos.Add(view);
            }

            return viewPhotos.AsEnumerable();

        }

        // GET: api/Photos/5
        [ResponseType(typeof(PhotoView))]
        public IHttpActionResult GetPhoto(int id)
        {
            var photo = db.Photos.Find(id);

            if (photo == null)
                return InternalServerError(new Exception("Photo not found"));

            var photoView = new PhotoView();
            photoView.Comments = new List<CommentView>();
            photoView.Likes = new List<LikeView>();

            photoView.Id = photo.Id;
            photoView.Image = photo.Image;
            photoView.Title = photo.Title;
            photoView.User = photo.User;
            photoView.Created = photo.Created;
            photoView.Location = photo.Location;

            foreach (var comment in photo.Comments)
            {
                var commView = new CommentView();
                commView.Id = comment.Id;
                commView.PhotoID = comment.PhotoID;
                commView.Text = comment.Text;
                commView.User = comment.User;
                photoView.Comments.Add(commView);
            }
            foreach (var like in photo.Likes)
            {
                var likeView = new LikeView();
                likeView.Id = like.Id;
                likeView.PhotoID = like.PhotoID;
                likeView.User = like.User;
                photoView.Likes.Add(likeView);
            }

            return Ok(photoView);
        }

        // PUT: api/Photos/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutPhoto(int id, Photo photo)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != photo.Id)
            {
                return BadRequest();
            }

            db.Entry(photo).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PhotoExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/Photos
        [ResponseType(typeof(Photo))]
        public IHttpActionResult PostPhoto(Photo photo)
        {
            PhotosRep rep = new PhotosRep();
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            rep.Insert(photo);

            return CreatedAtRoute("DefaultApi", new { id = photo.Id }, photo);
        }

        // DELETE: api/Photos/5
        [ResponseType(typeof(Photo))]
        public IHttpActionResult DeletePhoto(int id)
        {
            Photo photo = db.Photos.Find(id);
            if (photo == null)
            {
                return NotFound();
            }

            db.Photos.Remove(photo);
            db.SaveChanges();

            return Ok(photo);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool PhotoExists(int id)
        {
            return db.Photos.Count(e => e.Id == id) > 0;
        }
    }
}